<?php

namespace app\models;

use Yii;
use app\models\UserConfig;
use yii\web\UploadedFile;
use yii\helpers\Inflector;
use app\models\UploadForm;

/**
 * This is the model class for table "order".
 *
 * @property int $id 订单id
 * @property int $isp_id 运营商id
 * @property int $package_id 套餐id
 * @property string $price 单价
 * @property int $number 数量
 * @property int $type 卡类型
 * @property int $payment_type 付款类型
 * @property int $need_invoice 需要发票 1：需要 2：不需要
 * @property string $amount 金额
 * @property int $uid 用户编号
 * @property int $create_time 创建时间
 */
class Order extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'order';
    }
    public $title;
    public $username;
    public $agentname;

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['isp_id', 'package_id', 'number', 'type', 'payment_type', 'need_invoice', 'uid', 'create_time'], 'integer'],
            ['isp_id', 'exist', 'targetClass' => Isp::class, 'targetAttribute' => 'id'],

            ['package_id', 'exist', 'targetClass' => Package::class, 'targetAttribute' => 'id'],
            ['type', 'exist', 'targetClass' => Cardtype::class, 'targetAttribute' => 'id'],
            ['payment_type', 'exist', 'targetClass' => Cardtype::class, 'targetAttribute' => 'id'],
            ['active_time' ,  'filter', 'filter' => function(){ return strtotime($this->active_time); }],
            ['term_end_time' ,  'filter', 'filter' => function(){ return strtotime($this->term_end_time); }],
            [['price'], 'number'],
            [['id'], 'integer'],
            [['orderno'], 'string', 'max' => 255],
            [['amount'], 'string', 'max' => 255],
            ['title', 'string'],
            ['titles', 'string'],
            ['status', 'integer'],
            ['username', 'string'],
            ['agentname', 'string'],
            ['file', 'string'],
            ['term_length_unit', 'integer'],
            ['term_length', 'integer'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'orderno' => '订单号',
            'title' => '标题',
            'titles' => '订单名称',
            'isp_id' => '运营商 ID',
            'package_id' => '套餐 ID',
            'price' => '价格',
            'number' => '数量',
            'type' => '卡类型',
            'payment_type' => '支付方式',
            'need_invoice' => '是否需要发票',
            'amount' => '金额',
            'uid' => '用户',
            'agent' => '代理商',
            'create_time' => '创建时间',
            'file' => '上传凭证',
            'term_length_unit' => '套餐有效期单位',
            'term_length' => '套餐有效期',
        ];
    }
    /**
     * 关联运营商
     * @return \yii\db\ActiveQuery
     */
    public function getIsp()
    {
        return $this->hasOne(Isp::class, ['id' => 'isp_id']);
    }
    /**
     * 关联套餐
     * @return \yii\db\ActiveQuery
     */
    public function getPackage()
    {
        return $this->hasOne(Package::class, ['id' => 'package_id']);
    }
    /**
     * 关联用户
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::class, ['id' => 'uid']);
    }
    /**
     * 关联代理商
     * @return \yii\db\ActiveQuery
     */
    public function getAgent()
    {
        return $this->hasOne(Agent::class, ['id' => 'agent']);
    }
    /**
     * 关联卡类型
     * @return \yii\db\ActiveQuery
     */
    public function getCardtype()
    {
        return $this->hasOne(Cardtype::class, ['id' => 'type']);
    }
    /**
     * 生成新的批次号
     * @param integer $mid 管理用户编号，0表示系统自动添加
     * @return string
     */
    public static function generateBatchNo($mid = 0)
    {
        return date('YmdHis') . Util::randomStr(6) . $mid;
    }


}
